<template>
	<div class="container">
		<div class="header" :style="[headerStyle]" @click="navigateTo({ login: true, path: '/pages/sub/my/setting/index' })">
			<div class="navbar-state" :style='[stateBarStyle]'></div>
			<div class="navbar-nav" :style='[navbarBarStyle]'></div>
			<image class="header-bg" :src="$surl('/my/bg.jpg')" mode="aspectFill"></image>
			<view class="user-wrapper">
				<div class="user-wrapper-left">
					<image class="avatar" :src="userInfo['avatar']" />
					<view class="user-more" v-if="userInfo['_id']">
						<text class="username f-m">{{ userInfo.nickname || '注册/登录' }}</text>
						<view class="school"><text>ID:{{ userInfo['inviter_code'] }}</text></view>
					</view>
					<view class="login-box" v-else>
						<text>请登录</text>
					</view>
				</div>
				<i class='iconfont icon-right' v-if="userInfo['_id']"></i>
			</view>
			<image class="arc-line" :src="$surl('/my/arc.png')" mode="aspectFill" />
		</div>

		<div class="content">
			<view class="money-wrap">
				<view class="item" @click="navigateTo({ login: true, path: '/pages/sub/my/wallet/index' })">
					<text class="num">{{ userInfo['money'] || 0 }}</text>
					<text>钱包</text>
				</view>
				<view class="item" @click="navigateTo({ login: true, path: '/pages/sub/my/gold/index' })">
					<text class="num">{{ userInfo['gold'] || 0 }}</text>
					<text>金币</text>
				</view>
				<view class="item" @click="navigateTo({ login: true, path: '/pages/sub/my/score/index' })">
					<text class="num">{{ userInfo['score'] || 0 }}</text>
					<text>积分</text>
				</view>
			</view>

			<!-- 我的功能 -->
			<view class="list-box">
				<view class="head-list uv-border-bottom">
					<view class="title">我的功能</view>
				</view>
				<view class="main">
					<view class="item" v-for="(item, index) in myList" :key="index" @click="navigateTo(item)">
						<div class="item-iconfont">
							<i class="iconfont" :class="item['iconfont']"></i>
						</div>
						<text class="text">{{ item.name }}</text>
					</view>
				</view>
			</view>

			<!-- 更多工具 -->
			<view class="list-box">
				<view class="head-list uv-border-bottom">
					<view class="title">更多工具</view>
				</view>
				<view class="tools">
					<view class="item" v-for="(item, index) in toolsList" :key="index" @click='navigateTo(item)'>
						<div class='item-left'>
							<i class="iconfont" :class="item['iconfont']"></i>
							<text class="text">{{ item.name }}</text>
						</div>
						<i class='iconfont icon-right'></i>
					</view>
				</view>
			</view>
		</div>

		<kefuVue ref="kefuRef" />
		<AuxiliaryMenu :isBack='false' />
	</div>
</template>

<script setup>
import { computed, ref } from 'vue';
import userMenu from '@/common/userMenu';
import { onShow } from '@dcloudio/uni-app'
import { useUserStore } from '@/store/user.js';
import { useSystemStore } from '@/store/system.js'
import { useGeneralStore } from '@/store/general.js'
import kefuVue from './components/kefu.vue';
import AuxiliaryMenu from '@/components/AuxiliaryMenu/index.vue'

const userStore = useUserStore()
const systemStore = useSystemStore();
const generalStore = useGeneralStore();

const kefuRef = ref(false);

const userInfo = computed(() => userStore.userInfo);
const navbarInfo = computed(() => systemStore.navbarInfo);
const audit = computed(() => generalStore.systemData.base.audit);
const kefuQrcode = computed(() => generalStore.systemData.base.kefuQrcode);
const myList = computed(() => userMenu['my'].filter(item => !(audit.value && item['check'])))
const toolsList = computed(() => userMenu['tools'].filter(item => {
	if (item['path'] === 'kefu') {
		return kefuQrcode.value
	}
	return true;
}))

const stateBarStyle = computed(() => ({
	height: navbarInfo['value']['statusBar'] + 'px'
}));

const navbarBarStyle = computed(() => ({
	height: navbarInfo['value']['navigationBar'] + 'px',
}));

const headerStyle = computed(() => ({
	height: navbarInfo['value']['total'] + uni.upx2px(320) + 'px',
}))

function initData() {
	userStore.init()
}

function navigateTo(item) {
	if (item['path'] === 'kefu') {
		return kefuRef.value.open();
	}

	if (!userStore.token) return '';
	uni.navigateTo({ url: item['path'] })
}

onShow(() => {
	initData()
})
</script>

<style lang="scss" scoped>
.container {
	background: $uv-bg-color;

	.header {
		position: relative;
		z-index: 1;

		.header-bg {
			width: 100%;
			height: 100%;
			vertical-align: top;
			position: absolute;
			left: 0;
			top: 0;
			z-index: -1;
		}

		.user-wrapper {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			padding: 0 $side-width ;
			padding-top: 20rpx;

			.user-wrapper-left {
				display: flex;
				align-items: center;

				.avatar {
					flex-shrink: 0;
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					margin-right: 24rpx;
					border: 4rpx solid #fff;
					background-color: #fff;
				}

				.user-more {
					height: 100rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-around;

					.username {
						font-size: 32rpx;
						color: #fff;
						font-weight: 600;
						display: block;
					}

					.school {
						text {
							background-color: #fdf5d0;
							border-radius: 13px;
							color: $uv-main-color;
							font-size: 22rpx;
							height: 30rpx;
							line-height: 30rpx;
							padding: 0 12rpx;
							text-align: center;
						}
					}
				}

				.login-box {
					font-size: 34rpx;
					color: #fff;
				}
			}

			.icon-right {
				color: #fff;
				font-size: 26rpx;
			}
		}

		.arc-line {
			position: absolute;
			left: 0;
			bottom: 0;
			z-index: 1;
			width: 100%;
			height: 32rpx;
		}
	}

	.content {
		padding: 0 $side-width;
		position: relative;
		z-index: 2;
		margin-top: -100rpx;

		.money-wrap {
			display: flex;
			align-items: center;
			justify-content: space-around;
			width: 100%;
			padding: 36rpx 0;
			background: #fff;
			border-radius: 10rpx;

			.item {
				display: flex;
				align-items: center;
				flex-direction: column;
				justify-content: center;
				flex: 1;
				font-size: 24rpx;
				color: $uv-tips-color;

				.num {
					margin-bottom: 20rpx;
					font-size: 36rpx;
					font-weight: 600;
					color: $uv-main-color;
				}
			}
		}

		.list-box {
			border-radius: 12rpx;
			background-color: #fff;
			overflow: hidden;
			margin-top: 26rpx;

			.head-list {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 80rpx;
				padding: 0 $side-width;

				.title {
					font-weight: 500;
				}

				.more {
					display: flex;
					align-items: center;

					span {
						color: $uv-tips-color;
						font-size: 26rpx;
					}

					.icon-right {
						color: $uv-tips-color;
						font-size: 24rpx;
					}
				}
			}

			.main {
				display: flex;
				flex-wrap: wrap;
				text-align: center;
				padding-bottom: 30rpx;

				.item {
					width: 25%;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					align-items: center;
					margin-top: 20rpx;

					.item-iconfont {
						width: 80rpx;
						height: 80rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						position: relative;

						.iconfont {
							font-size: 54rpx;
							color: $uv-error;
						}
					}

					.text {
						color: $uv-tips-color;
						font-size: 24rpx;
						padding-top: 3rpx;
					}
				}
			}

			.tools {
				display: flex;
				flex-direction: column;
				padding-bottom: 10rpx;
				padding-top: 10rpx;

				.item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 20rpx 28rpx;

					.item-left {
						display: flex;
						align-items: center;

						.iconfont {
							font-size: 36rpx;
							margin-right: 10rpx;
						}

						.text {
							color: $uv-content-color;
						}
					}

					.icon-right {
						font-size: 26rpx;
						color: $uv-tips-color;
					}
				}
			}
		}
	}
}
</style>